<?php
include_once('../../php/postgre.php');

$act = '';
if(isset($_POST['act'])) $act = $_POST['act'];
if($act=='save_kegiatan'){
	$idkeg = $_POST['id_kegiatan'];
	//$idkla = $_POST['id_klasifikasi'];
	$idpro = $_POST['id_program'];
	$kdkeg = $_POST['kode_kegiatan'];
	$nmkeg = $_POST['nama_kegiatan'];
	$dampk = $_POST['dampak'];
	$ssran = $_POST['sasaran'];
	$pelak = $_POST['pelaksanaan'];
	$biaya = $_POST['biaya'];
	$waktu = $_POST['waktu'];
	
	if(!is_numeric($idkeg) || !is_numeric($idpro)){
		echo "Err: invalid id.";
		exit;
	}
	
	if($idkeg>0){
		//exec_query("update mus_kegiatan set id_klasifikasi=".$idkla.", id_program=".$idpro." ,kode_kegiatan='".$kdkeg."' ,nama_kegiatan='".$nmkeg."' where id_keg=".$idkeg);
		exec_query("update mus_kegiatan set id_program=".$idpro." ,kode_kegiatan='".$kdkeg."' ,nama_kegiatan='".$nmkeg."' where id_keg=".$idkeg);
		echo 'success__';
		
		logs('Mengubah Master Kegiatan',' Nama Kegiatan : '.$nmkeg);
	}else{
		$maxid = get_maxid('id_keg','mus_kegiatan');
		exec_query("insert into mus_kegiatan(id_keg,id_program,kode_kegiatan,nama_kegiatan) values(".$maxid.",".$idpro.",'".$kdkeg."','".$nmkeg."')");
		$stored = get_data('select id_keg from mus_kegiatan where id_keg='.$maxid);
		if($stored['id_keg']==$maxid){
			echo 'success__';
			$idkeg = $maxid;
			logs('Menambahkan kegiatan','ID Program '.$idpro.'; Nama Kegiatan : '.$nmkeg);
		}
	}
	
	echo $idkeg.'__';
	view_kegiatan();
	
}else if($act=='del_kegiatan'){
	$idkeg = $_POST['idkeg'];
	if(is_numeric($idkeg)){
		$kegg = get_data('select nama_kegiatan from mus_kegiatan where id_keg='.$idkeg);
		exec_query("delete from mus_kegiatan where id_keg=".$idkeg);
		echo 'success';
		logs('Menghapus kegiatan','Kegiatan : '.$kegg['nama_kegiatan']);
	}
	echo $idPro.'__';
	view_kegiatan();
}

else if($act=='edit_kegiatan'){
	$idkeg = $_POST['idkeg'];
	if(is_numeric($idkeg)){
		//$data = get_data("select id_keg,id_klasifikasi,id_program,kode_kegiatan,nama_kegiatan from mus_kegiatan where id_keg=".$idkeg);
		$data = get_data("select id_keg,mus_kegiatan.id_program,kode_kegiatan,nama_kegiatan,id_kla from mus_kegiatan,mus_program where mus_kegiatan.id_program=mus_program.id_program and id_keg=".$idkeg);
		$prog = get_datas("select id_program,kode_program,nama_program from mus_program where id_kla=".$data['id_kla']);
		$dpro = '__';
			$dpro .= '<option value="0">- Pilih Program -</option>';
		foreach($prog as $prog){
			$dpro .= '<option value="'.$prog['id_program'].'">'.$prog['nama_program'].'</option>';
		}
		
		$ress = implode($data,'__');
		print_r($ress);
		echo $dpro;
	}else{
		echo 'error';
	}
}else if($act=='get_program'){
	$idkla = $_POST['idkla'];
	if(is_numeric($idkla)){
		$program = get_datas("select id_program,kode_program,nama_program from mus_program where id_kla=".$idkla." order by id_program");
		echo '<option value="0" id="idpro">- Pilih Program -</option>';
		foreach($program as $program){
		?>
		<option value="<?php echo $program['id_program']; ?>"><?php echo $program['nama_program']; ?></option>
		<?php
		}
	}else{
		echo 'error';
	}
}else if($act=='get_kode'){
	$kode= $_POST['kode'];
	if(!is_numeric($kode)) exit();
	$kodepr = get_data("select kode_program from mus_program where id_program=".$kode);
	$kodepro = $kodepr['kode_program'];
	
	$kodekg =get_data("select count(id_keg) as mac from mus_kegiatan where id_program='$kode'");
	$kdkk = $kodekg['mac'] + 1;
	$kdkeg = ($kdkk <=9)? "0".$kdkk:$kdkk;
	$kode_keg = $kodepro.".".$kdkeg;
	echo $kode_keg;
}



function view_kegiatan(){
	$kegiatan = get_datas("select * from mus_kegiatan order by kode_kegiatan");
	$no=1;
	foreach($kegiatan as $kegiatan){
	?>
	<tr><td style="text-align:center;"><?php echo $no?></td>
		<td><?php echo $kegiatan['kode_kegiatan']; ?></td>
		<td><?php echo $kegiatan['nama_kegiatan']; ?></td>
		<td class="center">
			<a href="#modalwin" data-toggle="modal" class="btn btn-info btn-small bt-edit-kegiatan" name="<?php echo $kegiatan['id_keg']; ?>"><i class="icon-edit icon-white"></i>Ubah</a>
			<a class="btn btn-danger btn-small bt-delete-kegiatan" name="<?php echo $kegiatan['id_keg']; ?>"><i class="icon-trash icon-white"></i>Hapus</a>
		</td>
	</tr>
	<?php
	$no++;
	}
}
?>